
local gMaxSlotCount=10
local Bro_Cfg=_G.CommonOHOH.config.Bro

local M={}

function M.Calculate(bro)
    local upCount,upkey=M.Calculate_upgrade(bro)
    local seCount,sekey=M.Calculate_sell(bro)
    bro["power"]=M.Calculate_power(bro)
    bro["nextPower"]=M.Calculate_nextPower(bro)
    bro["upgrade"]={[upkey]=upCount}
    bro["sell"]={[sekey]=seCount}
    return bro
end

function M.Calculate_power(oneBro)
    local cfg=M.GerBroConfig(oneBro.baseLevel,oneBro.level)
    return cfg.damge
end
function M.Calculate_nextPower(oneBro)
    local cfg=M.GerBroConfig(oneBro.baseLevel,oneBro.level+1)
    return cfg.damge
end

function M.Calculate_upgrade(oneBro)
    local cfg=M.GerBroConfig(oneBro.baseLevel,oneBro.level+1)
    for k,v in pairs(cfg.awake) do
        return v,k
    end
  
end

function M.Calculate_sell(oneBro)
    local cfg=M.GerBroConfig(oneBro.baseLevel,oneBro.level)
    for k,v in pairs(cfg.sell) do
        return v,k
    end
end

function M.Calculate_awake(oneBro)
    local cfg=M.GerBroConfig(oneBro.baseLevel,oneBro.level)
    for k,v in pairs(cfg.awake) do
        return v,k
    end
end

-- 根据战力排序，返回Idlist
function M.SortByPower(bro)
    local powerList={}
    for k,v in pairs(bro) do
        table.insert(powerList,{
            id=k,
            power=M.Calculate_power(v)
        })
    end
    table.sort(powerList,function(a,b)
        return a.power>b.power
    end)
    return powerList
end

--能否装备  1：槽位已经满，2，去购买槽位，3 未知错误
function M.IsCanEquip(idList,equip,broSlot)
    local add=#idList
    local now=#equip
    local count=broSlot
    if now+add<=count then
        return 0
    end
    if broSlot<gMaxSlotCount then
        return 2
    end
    return 1
end

-- 能否购买槽位
function M.IsCanBuySolt(broSlot)
    return broSlot<gMaxSlotCount
end

--能否升级
function M.IsCanUpgrade(idList,bro,dataBase)
    local sum=0
    local adder,key
    for _,id in pairs(idList) do
        if not bro[id] then --不存在该Id
            return 3
        end
        adder,key= M.Calculate_upgrade(bro[id])
        if adder==0 then --存在满级的，不能升级

            return 2
        end
        sum=sum+adder

    end

    if  _G.CommonOHOH.load.ItemTool().IsHadEnough(key,sum,dataBase) then
            return 0,sum,key
    else
        return 1
    end
end


--能否激活某一个好友
function M.IsCanAwake(chapterId,levelId,dataBase)

    local id=M.Calculate_cfg_level2(chapterId,levelId,1)
   local cfg= Bro_Cfg[id]

   for key,sum in pairs(cfg.awake) do
        if  _G.CommonOHOH.load.ItemTool().IsHadEnough(key,sum,dataBase) then
            return 0,sum,key
        else
            return 1
        end
   end
end

function M.GerBroConfig(baseLevel,uplevel)
    assert(baseLevel,"baseLevel==nil")
    assert(uplevel,"uplevel==nil")
    local id=M.Calculate_cfg_level(baseLevel,uplevel)
    return Bro_Cfg[id]
end

function M.Calculate_cfg_level(baseLevel,uplevel)
    local a,b=M.GetChapterIdAndIndex(baseLevel)
    return M.Calculate_cfg_level2(a,b,uplevel)
end

function M.Calculate_cfg_level2(chapterId,index,uplevel)
    return (chapterId-1)*6+index+(uplevel-1)
end

--根据章节ID，及位置等级，计算好友的等级
function M.GetBaseLevel(chapterId,levelId)
    return chapterId.."_"..levelId
end

--根据章节ID，及位置等级，计算好友的等级
function M.GetChapterIdAndIndex(baseLevel)
    local arg=string.split(baseLevel,"_")
    return tonumber(arg[1]),tonumber(arg[2])
end

return M